#include <opencv2/opencv.hpp>  
#include "FaceDetector.h"  

int main(int argc, char** argv) {  
    if (argc != 3) {  
        std::cerr << "Usage: " << argv[0] << " <video_path> <output_path> <face_cascade_path>" << std::endl;  
        return -1;  
    }  

    std::string video_path = argv[1];  
    std::string output_path = argv[2];  
    
    // 传入对应的级联分类器路径  
    FaceDetector detector("../data/haarcascades/haarcascade_frontalface_default.xml", "../data/haarcascades/haarcascade_eye.xml", "../data/haarcascades/haarcascade_smile.xml");  

    cv::VideoCapture capture(video_path);  
    if (!capture.isOpened()) {  
        std::cerr << "Error opening video file: " << video_path << std::endl;  
        return -1;  
    }  

    int frame_width = static_cast<int>(capture.get(cv::CAP_PROP_FRAME_WIDTH));  
    int frame_height = static_cast<int>(capture.get(cv::CAP_PROP_FRAME_HEIGHT));  
    int fps = static_cast<int>(capture.get(cv::CAP_PROP_FPS));  

    cv::VideoWriter writer(output_path, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, cv::Size(frame_width, frame_height));  

    if (!writer.isOpened()) {  
        std::cerr << "Error opening output video file: " << output_path << std::endl;  
        return -1;  
    }  

    cv::Mat frame;  
    while (true) {  
        capture >> frame;  
        if (frame.empty()) break;  

        detector.detectAndDisplay(frame);  
        writer.write(frame);  
        cv::imshow("Face Detection", frame);  
        if (cv::waitKey(30) >= 0) break;  
    }  

    capture.release();  
    writer.release();  
    cv::destroyAllWindows();  
    return 0;  
}